package org.jeecg.modules.simulator.rooter;

import lombok.Getter;
import lombok.Setter;
import org.jeecg.modules.simulator.factory.DataBizFactory;
import org.jeecg.modules.simulator.jsonot.RouteStation;
import org.jeecg.modules.simulator.model.line.station.station.Station;
import org.jeecg.modules.simulator.model.line.station.station.TransferStation;
import org.jeecg.modules.simulator.model.person.path.PersonPath;
import org.jeecg.modules.simulator.model.person.path.PersonPathItem;
import org.jeecg.modules.simulator.service.HistoryBizPersonLoaderService;
import org.jeecg.modules.simulator.service.HistoryBizTrainLoaderService;
import org.jeecg.modules.simulator.service.T80RouteHisService;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: simulator-b20240918
 * @description:
 * @author: renzhg
 * @create: 2025-01-09 09:06
 **/
public class DataLoadRooter {

    @Getter
    @Setter
    HistoryBizTrainLoaderService historyBizTrainLoaderService;

    @Getter
    @Setter
    HistoryBizPersonLoaderService historyBizPersonLoaderService;

    @Getter
    @Setter
 NetRooter netRooter;


    @Getter
    @Setter
    T80RouteHisService t80RouteHisService;


    DataBizFactory dataBizLoader = new DataBizFactory();

    public void loadBizData(){
        RooterBizLoader rooterBizLoader = getRooterBizLoader();
        netRooter.trainPlanTableMapByLine = rooterBizLoader.getTrainPlanTableMapByLine();
        netRooter.LoadPersons();

        //注释掉，单独提出来
//        netRooter.personTrainMapByLine = rooterBizLoader.getPersonTrainMapByLine();
//        netRooter.odProbabilityApiInfoMap = rooterBizLoader.getOdProbabilityApiInfoMap();
//        netRooter.numPeopEnStationApiInfoMap = rooterBizLoader.getNumPeopEnStationApiInfoMap();
//        netRooter.psgStationDistributionInfoMap = rooterBizLoader.getPsgStationDistributionInfoMap();
//        generateHistoryPath(netRooter.getT80RouteHisMap());

    }

    private boolean isInTranserStation(String stationId) {
        boolean isInTranserStation = false;
        for (Map.Entry<String, TransferStation> entry : netRooter.transferStationMap.entrySet()) {
            if (entry.getValue().getTransferStationMap().containsKey(stationId)) {
                isInTranserStation = true;
                break;
            }
        }
        return isInTranserStation;
    }

    private List<PersonPathItem> setPersonPathItemTransfered(List<PersonPathItem> personPathItemList) {
        personPathItemList.sort(Comparator.comparingInt(PersonPathItem::getIndex));
        for (int i = 1; i < personPathItemList.size(); i++) {
            PersonPathItem previous = personPathItemList.get(i - 1);
            PersonPathItem current = personPathItemList.get(i);
            if (previous.getStationName().equals(current.getStationName())) {
                previous.setNeedTransfer(true);
            }
        }

        return personPathItemList;

    }
    public void generateHistoryPath(Map<String, List<RouteStation>> t80RouteHisMap) {
        List<PersonPath> personPaths = new ArrayList<>();


        t80RouteHisMap.forEach((k, v) -> {
            v.sort(Comparator.comparingInt(rs -> Integer.parseInt(rs.stationSn)));
            boolean isInPath = false;
            List<PersonPathItem> personPathItemList = new ArrayList<>();
            for (int i = 0; i < v.size(); i++) {
                PersonPathItem personPathItem = new PersonPathItem();
                personPathItem.setIndex(Integer.parseInt(v.get(i).getStationSn()));
                personPathItem.setPathIndex(v.get(i).getStationSn());
                personPathItem.setStationId(v.get(i).getStationId());
                if (netRooter.stationMap.get(v.get(i).getStationId()) == null) {
                    isInPath = true;
                } else {
                    Station station = netRooter.stationMap.get(v.get(i).getStationId());
                    personPathItem.setStationName(station.getName());
                    personPathItem.setLineId(station.getLine());
                    if (isInTranserStation(station.getId())) {
                        personPathItem.setTransfer(true);
                    }
                    personPathItem.setNeedTransfer(false);
                }
                personPathItemList.add(personPathItem);
            }
            if (!isInPath) {
                List<PersonPathItem> personPathItemList1 = setPersonPathItemTransfered(personPathItemList);
                PersonPath personPath = new PersonPath(personPathItemList1);
                netRooter.personPathMap.put(k + "_" + personPath.getFirst().getStationName(), personPath);
                netRooter.personPathIdMap.put(k + "_" + personPath.getFirst().getStationId(), personPath);
                personPaths.add(personPath);


            }
        });
        netRooter.historyPersonPathMap = personPaths.stream().collect(Collectors.groupingBy(e -> e.getFirst().getStationName()));


//        historyPersonPathMap.put(k, personPaths);
    }

    /**
     * @description: 加载业务数据，包括列车时刻表、OD概率数据和站点数据。
     *
     * 1、创建一个新的 RooterBizLoader 实例
     * 2、设置数据业务加载器
     * 3、设置历史业务列车加载服务
     * 4、设置历史业务人员加载服务
     * 5、设置路径信息服务的初始化信息，用来查询历史路径信息
     * 6、设置换乘站映射
     * 7、设置站点映射
     * 8、设置场景代码
     * 9、设置停车场映射
     * 10、设置停车场映射
     * 11、加载业务数据
     *
     * @author:  renzhg
     * @date:  2024/11/29 10:29
     **/
    private @NotNull RooterBizLoader getRooterBizLoader() {
        RooterBizLoader rooterBizLoader = new RooterBizLoader();
        rooterBizLoader.setDataBizLoader(dataBizLoader);
        rooterBizLoader.setHistoryBizTrainLoaderService(historyBizTrainLoaderService);
        rooterBizLoader.setHistoryBizPersonLoaderService(historyBizPersonLoaderService);
//        rooterBizLoader.setT80RouteHisService(t80RouteHisService);
        rooterBizLoader.setTransferStationMap(netRooter.transferStationMap);
        rooterBizLoader.setStationMap(netRooter.stationMap);
        rooterBizLoader.setSceneCode(netRooter.sceneCode);
        rooterBizLoader.setParkersMap(netRooter.parkersMap);
        rooterBizLoader.setParkerMap(netRooter.parkerMap);
        rooterBizLoader.loadBizData();

        return rooterBizLoader;
    }
}
